package core;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

import main.SYRRES;

public class Beer {
	
	private String id;
	private HashMap<String, List<Integer>> userNote;
	private HashMap<String, Integer> beerWeight;
	private int sumNote[] = new int[SYRRES.NB_NOTE];
	private double average[] = new double[SYRRES.NB_NOTE];
	
	public Beer(String id) {
		this.id = id;
		this.userNote = new HashMap<String, List<Integer>>();
		this.beerWeight = new HashMap<String, Integer>();
		for (int i = 0; i < average.length; i++) {
			average[i] = -2;
		}
	}
	
	public Beer(String id, HashMap<String, List<Integer>> userNote) {
		this.id = id;
		this.userNote = userNote;
		this.beerWeight = new HashMap<String, Integer>();
	}
	
	public void addUser(String user, List<Integer> note) {
		userNote.put(user, note);
	}
	
	public void addBeer(String beer, int weight) {
		beerWeight.put(beer, weight);
	}
	
	public boolean canConnectWith(Set<String> beers, int threshold) {
		if (beers.size() <= threshold) 	return false;
		for (String beer : beers) {
			if (!beerWeight.keySet().contains(beer)) {
				return false;
			}
		}
		return true;
	}
	
	public void clearUser() {
		userNote.clear();
	}
	
	public HashMap<String, List<Integer>> getUserNote() {
		return userNote;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}	
	
	public void calculSumNote(int noteID) {
		int sum = 0;
		for (List<Integer> notes : userNote.values()) {
			sum += notes.get(noteID);
		}
		sumNote[noteID] = sum;
	}
	
	public int getSumNote(int noteID) {
		return sumNote[noteID];
	}
	
	public double getAverageNote(int noteID) {
		if (average[noteID] == -2) {
			average[noteID] = (double)sumNote[noteID] / userNote.size();
		}
		return average[noteID];
	}
	
	public void addAverageNote(int noteID, double value) {
		average[noteID] += value;
	}
}
